var $ = require('webpack-zepto');
var config = require('../../common/config.js');
var util = require('../../common/util.js');
var Area = require('./area.js');
function AddressApi(){

    const url = config.path("duolu/address", ['?uToken=' + util.storage().get(util.appId())]);
    var scope = this;

    this.saveOrUpdate = function(type,data,callback){
        $.ajax({
            url: url,
            type: type,
            data: data,
            dataType: 'json',
            async: false,
            success: function (resp) {
                if (resp.status == 200) {
                    callback.call();
                }else{
                    alert(resp.status);
                }
            }
        });
    };

    this.emptyAddress = function(){
        return {
            address : '',
            area_id : '',
            city : '',
            contact : '',
            district : '',
            is_default : 0,
            province : '',
            tel : ''
        };
    };
    this.validate = function(address){
        address.isChoose=true;
        var result = {
            key : 'success',
            value :true
        };
        for(var i in address){

            if('district' == i || 'is_default' == i){
                continue;
            }

            if( (!address[i] || address[i] == '')){
                result.key = i;
                result.value = false;
                break;
            }
        }
        return result;
    };

    this.list = function(){
        var list = [];
        $.ajax({
            url: url,
            type: 'get',
            dataType: 'json',
            async : false,
            success: function (resp) {
                if (resp.status == 200) {
                    list = resp.data;
                }else if (resp.status == 401) {
                    window.location.href = 'index.html?appid=' + util.appId();
                }else if(resp.status == 404){
                    util.storage().put('address',scope.emptyAddress());
                    window.location.href = 'address.html?type=form&appid=' + util.appId();
                }
            }
        });
        return list;
    };

    this.delete = function(id){
        $.ajax({
            url: url + '&id=' + id,
            type: 'delete',
            dataType: 'json',
            async: false,
            success: function (resp) {
                if (resp.status == 200) {
                    window.location.reload();
                }
            }
        });
    };

    this.getAddressArea = function(address){
        var district = Area.getArea(address.area_id);
        var city = Area.getArea(district.area_parent_id);
        var province = Area.getArea(city.area_parent_id);

        return {
            province : province.area_name,
            city : city.area_name,
            district : district.area_name
        };
    };

    function setAreaSelected(areaList,name){
        var list = [];
        for(var i = 0 ; i < areaList.length; i++){
            if(areaList[i].area_name.indexOf(name) != -1){
                areaList[i].selected = true;
                list[0] = areaList[i];
                break;
            }
        }

        for(var i = 0 ; i < areaList.length; i++){
            if(!areaList[i].selected){
                list.push(areaList[i]);
            }
        }

        return list;
    };

    this.getChildAreaList = function(pId){
        return Area.getAreaList(pId);
    };

    this.getAreaListByArea = function(area){
        var provinces = Area.getAreaList(0);//所有的省
        var city = Area.getCityByProvince(area.province);//某省下所有的城市
        var districts = Area.getDistrictsByCity(area.city);
        provinces = setAreaSelected(provinces,area.province);
        city = setAreaSelected(city,area.city);
        districts = setAreaSelected(districts,area.district);

        return {
            provinces : provinces,
            city : city,
            districts : districts,
            default : area
        }
    };


    this.getDefaultAreaList = function(){
        var provinces = Area.getAreaList(0);
        var city = Area.getAreaList(provinces[0].area_id);
        var districts = Area.getAreaList(city[0].area_id);
        return {
            provinces : provinces,
            city : city,
            districts : districts
        }
    };
};
module.exports = new AddressApi();